package it.appcatalogobase;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import it.appcatalogobase.R;
 
public class ImageAdapter extends BaseAdapter {
	private Context context;
	private final String[] productsValues;
	private final String[] productsIds;
	private final String[] productsImages;
 
	public ImageAdapter(Context context, String[] prValues, String[] prIds, String[] prImages) {
		this.context = context;
		this.productsIds = prIds;
		this.productsValues=prValues;
		this.productsImages=prImages;
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
//		LayoutInflater inflater = (LayoutInflater) context
//			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 
//		View gridView;
// 
//		if (convertView == null) {
// 
//			gridView = new View(context);
// 
//			// get layout from mobile.xml
//			gridView = inflater.inflate(R.layout.prodotto, null);
// 
//			// set value into textview
//			TextView textView = (TextView) gridView
//					.findViewById(R.id.grid_item_label);
//			textView.setText(productsValues[position]);
//			TextView textViewId = (TextView) gridView
//					.findViewById(R.id.grid_item_id);
//			textViewId.setText(productsIds[position]);
// 
//			// set image based on selected text
//			ImageView imageView = (ImageView) gridView
//					.findViewById(R.id.grid_item_image);
//			try {
//				imageView.setImageBitmap(BitmapFactory.decodeStream(context.openFileInput(productsImages[position])));
//			} catch (IOException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//			String productImage = productsImages[position];
//		    // get input stream
//		    InputStream ims = null;
//			try {
//				ims = context.openFileInput(productImage);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		    // load image as Drawable
//			Drawable d =null;
//			if(ims!=null)
//				d = Drawable.createFromStream(ims, null);
//		} else {
//			gridView = (View) convertView;
//		}
 
		View myView = convertView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            //Inflate the layout
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            myView = li.inflate(R.layout.prodotto, null);
       } else {
    	   myView = convertView;
          }
            // Add The Image!!!           
        TextView textView = (TextView) myView
				.findViewById(R.id.grid_item_label);
		textView.setText(productsValues[position]);
		TextView textViewId = (TextView) myView
				.findViewById(R.id.grid_item_id);
		textViewId.setText(productsIds[position]);

		// set image based on selected text
		ImageView imageView = (ImageView) myView
				.findViewById(R.id.grid_item_image);
		try {
			imageView.setImageBitmap(BitmapFactory.decodeStream(context.openFileInput(productsImages[position])));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String productImage = productsImages[position];
	    // get input stream
	    InputStream ims = null;
		try {
			ims = context.openFileInput(productImage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    // load image as Drawable
		Drawable d =null;
		if(ims!=null)
			d = Drawable.createFromStream(ims, null);
            return myView;
    }
		
 
	@Override
	public int getCount() {
		return productsValues.length;
	}
 
	@Override
	public Object getItem(int position) {
		return null;
	}
 
	@Override
	public long getItemId(int position) {
		return 0;
	}
 
}